package threading;

/**
 * @author krystal
 * @date 2022/11/24 11:58
 */
public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        //准备一个对象，保证等待和调用是同一个
        Object object=new Object();

        //第一个线程
        Thread t1=new Thread(()->{
            while (true){
                synchronized (object){
                    System.out.println("wait之前");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("wait之后");
                }
            }
        });
        t1.start();
        Thread.sleep(500);

        //第二个线程，进行notify
        Thread t2=new Thread(()->{
            while (true){
                synchronized (object){
                    System.out.println("notify之前");
                    object.notify();
                    System.out.println("notify之后");
                }

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });
        t2.start();
    }
}
